// pages/document/detail.js

const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    fontSize: 16,
    id: 0,
    detail: {},
    user: {},
    article_content: {},
    loaddinghidden: true,
    columnIndex: 0,
    columnID: 0,
    columnArray: [],
    columnNameArray: [],
    cyBtnShow: false,
    cyViewShow: false,
    showLoading: false,
    isCollected: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options 为页面跳转所带来的参数
    var that = this;
    var id = options.id;
    if (!id) {
      wx.showModal({
        title: '加载失败！',
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    }

    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    var user = wx.getStorageSync("user");
    var user_role = wx.getStorageSync("user_role");

    if (user) {
      var cyBtnShow = false;
      if (user.is_reporter == 0 && user_role == "editor") {
        cyBtnShow = true;
      }

      that.setData({
        user: user,
        cyBtnShow: cyBtnShow
      });
    }

    // 显示 loading
    that.setData({
      id: id,
      loaddinghidden: false
    });

    that.loadData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var fontSize = wx.getStorageSync("fontSize");

    if (fontSize) {
      this.setData({
        fontSize: fontSize,
      });
    }
    this.checkCollect();
  },
  loadData: function () {
    var that = this;
    var parameter = {};
    parameter['id'] = that.data.id;
    //获取数据
    http.ApiGet(apiUrl.UrlDocumentDetail, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        that.historyAdd();
        var article_content = res.data.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;padding-top:2px;" ')
          .replace(/<section/g, '<div')
          .replace(/\/section>/g, '\div>');
        that.setData({
          loaddinghidden: true,
          detail: res.data,
          article_content: article_content
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadData();
  },
  checkCollect: function () {
    var that = this;

    var parameter = {};
    parameter['id'] = that.data.id;
    parameter['type'] = "4";

    http.ApiGet(apiUrl.UrlCheckCollection, parameter, function (res) {
      if (res.code == 1) {
        that.setData({
          isCollected: true,
        });
      } else {
        that.setData({
          isCollected: false,
        });
      }
    });
  },
  toEdit: function () {
    var that = this;
    var id = that.data.id;
    wx.navigateTo({
      url: '../document/submit?id=' + id
    })
  },
  toCollect: function () {
    var that = this;
    var formData = {};
    formData['id'] = that.data.id;
    formData['type'] = "4";
    http.ApiPost(apiUrl.UrlDoCollection, formData, function (res) {
      if (res.code == 1) {
        wx.showToast({
          title: res.msg
        });
        that.checkCollect();
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  historyAdd: function () {
    var that = this;
    var formData = {};
    formData['id'] = that.data.id;
    formData['type'] = "4";
    http.ApiPost(apiUrl.UrlHistoryAdd, formData, function (res) {
      if (res.code == 1) {
        console.log(res.msg);
      } else {
        // wx.showModal({
        //   title: res.msg
        // });
        console.log(res.msg);
      }
    });
  },
  imageTap: function () {
    var that = this;
    /*
    let arr = [];
    let reg = /(?<=(src="))[^"]*?(?=")/ig;
    // let reg = new RegExp('(?<=(src="))[^ "]*?(?=")');
    let allSrc = that.data.article_content.match(reg);
    if (allSrc&&allSrc.length > 0) {
      for (let i = 0; i < allSrc.length; i++) {
        console.log("e000", allSrc[i])
        arr.push(allSrc[i])
        that.setData({
          imageArray: arr
        })
      }
    }

    if (that.data.imageArray && that.data.imageArray.length > 0) {
      wx.previewImage({
        current: that.data.imageArray, // 当前显示图片的http链接  
        urls: that.data.imageArray // 需要预览的图片http链接列表  
      })
    }
    */
  },
  cyBtnTap: function () {
    var that = this;
    that.setData({
      cyViewShow: true
    });
    that.loadColumnData();
  },
  loadColumnData: function () {
    var that = this;
    var parameter = {};
    parameter['all'] = 0;
    //获取数据
    http.ApiGet(apiUrl.UrlColumnTree, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var newData = res.data;
        if (newData.length > 0) {
          var columnName = [];
          var columnID = newData[0].id;

          newData.map(function (item) {
            columnName.push(item.text);
          });
          that.setData({
            columnID: columnID,
            columnArray: newData,
            columnNameArray: columnName
          });
        }
      }
    });
  },
  columnPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var columnArray = this.data.columnArray;
    this.setData({
      columnIndex: e.detail.value,
      columnID: columnArray[e.detail.value].id
    })
  },
  columnBtnTap: function (e) {
    var index = e.currentTarget.dataset.index;
    var columnArray = this.data.columnArray;
    this.setData({
      columnIndex: index,
      columnID: columnArray[index].id
    })
  },
  okBtnTap: function () {
    var that = this;
    that.cancelBtnTap();

    if (that.data.columnID == 0) {
      wx.showModal({
        title: "请选择栏目"
      });
      return;
    }

    var formData = {};
    formData['document_id'] = that.data.id;
    formData['column_id'] = that.data.columnID;
    http.ApiPost(apiUrl.UrlDocumentUse, formData, function (res) {
      if (res.code == 1) {
        console.log(res.msg);
        wx.showModal({
          title: "采用成功"
        });
      } else {
        wx.showModal({
          title: res.msg
        });
        console.log(res.msg);
      }
    });

  },
  editBtnTap: function () {
    var that = this;
    that.cancelBtnTap();
    if (that.data.columnID == 0) {
      wx.showModal({
        title: "请选择栏目"
      });
      return;
    }
    wx.navigateTo({
      url: '../news/submit?did=' + that.data.id + '&cid=' + that.data.columnID
    });
  },
  cancelBtnTap: function () {
    var that = this;
    that.setData({
      cyViewShow: false
    });
  },
  copyTap: function (e) {
    var val = e.currentTarget.dataset.val;
    if (val.length > 0) {
      wx.setClipboardData({
        data: val,
        success: function (res) {
          console.log(res);
        }
      });
    }
  },
})